package com.itheima.service.impl;

import com.itheima.dao.AdminDao;
import com.itheima.domain.Admin;
import com.itheima.service.AdminService;
import com.itheima.utils.TransactionManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.List;

@Service(value = "adminService")
public class AdminServiceImpl implements AdminService {


    @Autowired
    @Qualifier(value = "adminDao")
    private AdminDao adminDao;


//    @Autowired
//    @Qualifier("transactionManager")
//    private TransactionManager transactionManager;


    public void setAdminDao(AdminDao adminDao) {
        this.adminDao = adminDao;
    }

    @Override
    public List<Admin> findAll() {
        List<Admin> all = adminDao.findAll();
        return all;
    }

    @Override
    public Admin findOne(Integer id) {
        Admin one = adminDao.findOne(id);
        return one;
    }

    @Override
    public Integer save(Admin admin) {
        Integer save = adminDao.save(admin);
        return save;
    }

    @Override
    public Integer update(Admin admin) {
        Integer update = adminDao.update(admin);
        return update;
    }

    @Override
    public Integer delete(Integer id) {
        Integer delete = adminDao.delete(id);
        return delete;
    }

    @Override
    public void transfer(String sourceName, String targetName, Double money) throws SQLException {
            //1.查出转出账户
            Admin source = adminDao.findByName(sourceName);
            //2.查出转入账户
            Admin target = adminDao.findByName(targetName);
            //3.减钱 加钱
            source.setMoney(source.getMoney() - money);
            target.setMoney(target.getMoney() + money);
            //4.更新账户
            Integer save1 = adminDao.update(source);
            int a = 10/0;
            Integer save2 = adminDao.update(target);

    }
}
